var KZ = KZ || {};
KZ = {
    form: {
        //透视提示
        Prompt: function(text, num) {
            var html = $('<div class="kz_tishi" style="position:absolute;z-index:9999;"/>').html(text)
            if ($(".kz_tishi").length == 0) {
                html.appendTo("body").delay(1500).fadeOut(500, function() { $(this).remove(); })
                html.css({
                    left: num == 1 ? ($(window).width() - html.outerWidth()) / 2 : 0,
                    top: num == 1 ? ($(window).height() - html.outerHeight()) / 2 : 0,
                    "width": num == 1 ? "auto" : "100%",
                    "position": "fixed"
                })
            }
        }
    },
    cookie: {
        get: function(_name) {
            var cookies = document.cookie,
                pos = cookies.indexOf(_name + '='),
                value;
            if (pos !== -1) {
                var start = pos + _name.length + 1;
                var end = cookies.indexOf(';', start);
                //最后一个
                if (end === -1) { end = cookies.length; }
                value = cookies.substring(start, end);
                value = decodeURIComponent(value);
            }
            return value;
        },
        set: function(_name, _value, minutes) {
            if (minutes) {
                var expi = new Date();
                expi.setMinutes(minutes);
                document.cookie = _name + '=' + encodeURIComponent(_value) + ';expires=' + expi.toGMTString();
            } else {
                document.cookie = _name + '=' + encodeURIComponent(_value);
            }
        },
        clearcookie: function(_name) {
            document.cookie = _name + "=;expires=" + (new Date(0)).toGMTString();
        }
    },
    pageLock: {
        show: function(index, ceng, colse, cfun) {
            if (!$("#lockpage").length) {
                var $width = ($(window).width() - $(ceng).outerWidth()) / 2
                var $height = ($(window).height() - $(ceng).outerHeight()) / 2
                var lockhtml = $("<div id='lockpage'/>").css({ position: "absolute", zIndex: (index ? index : 20), left: 0, top: 0, width: $(window).width(), height: $(document).height(), background: "#000", opacity: 0.3 }).appendTo($(".pageconversionwrap").length > 0 ? ".pageconversionwrap" : "body");
            }
            $("#lockpage").css({ opacity: 0, display: "block" }).animate({ opacity: 0.3 }, 500);
            $(ceng).css({ position: "fixed", left: $width, top: $height });
            $(ceng).show();
            $("#lockpage").click(function(e) {
                KZ.pageLock.hide(ceng)
                e.stopPropagation();
            })
            $(colse).click(function(e) {
                KZ.pageLock.hide(ceng)
                e.stopPropagation();
                return false
            });
            if (typeof cfun == "function") {
                cfun()
            }
        },
        hide: function(ceng) {
            $("#lockpage").remove()
            $(ceng).hide();
        }
    }
}
function ErrorMessage(o,text) {
   $(o).parents(".Rcheck").find(".error_hint").remove();
   var html = "<div class='error_hint'>" + text + "</div>"
   if (!$(o).parents(".Rcheck").find(".error_hint").length > 0) {
       $(html).appendTo($(o).parents(".Rcheck"))
       if ($(o).hasClass("required")) {
           $(o).addClass("kz_error")
       } else {
           $(o).parents(".required").addClass("kz_error")
       }
       return false
   }
}
function ErrorTishi(o,text) {
   $(o).find(".kz_tishi").remove();
   if (!$(o).parents(".Rcheck").find(".kz_tishi").length > 0 && $(".error_hint:visible").length<1) {
       KZ.form.Prompt(text, 1);
       return false
   }
}

function ValiSuccess(o) {
   $(o).parents(".Rcheck").find(".error_hint").remove()
   if ($(o).hasClass("required")) {
       $(o).removeClass("kz_error")
   } else {
       $(o).parents(".required").removeClass("kz_error")
   }
}
window.global = {
  preview: function(){
    var preview = document.getElementById('LAY_preview');
    return preview ? preview.innerHTML : '';
  }()
};
;
layui.use(['element', 'layer'], function(){
  var element = layui.element()
  ,layer = layui.layer;
  element.on('tab(docDemoTabBrief)', function(data){
    layer.msg('切到到了'+ data.index + '：' + this.innerHTML);
  });
});
layui.use('laydate', function(){
  
});

//全选
layui.use('form', function(){
  var $ = layui.jquery, form = layui.form();
  //全选
  form.on('checkbox(allChoose)', function(data){
    var child = $(data.elem).parents('.layui-form').find('ul input[type="checkbox"]');
    child.each(function(index, item){
      item.checked = data.elem.checked;
    });
    form.render('checkbox');
  });
  
});
$(function(){
	//加载更多
	$(".load_more").each(function(index, element) {
		var T=$(this);
		var url=T.data("url");
		T.click(function(){
			    var lastDate=T.attr("data-time");
				if(lastDate>0){
					   $.ajax({
							url: url,
							data:{"lastDate":lastDate},
							type: "get",
							dataType: "json",
							success: function(data) {
								if(data.rescode==0){
									if(data.hasMore){
										$(data.html).appendTo(".info")
										T.attr("data-time",data.lastDate)
									}
									else
									{
										$(data.html).appendTo(".info");
										T.html("数据加载完毕").fadeOut(1000)
										T.attr("data-time",0)
									}
									
								}
								else
								{
									
								}
							},
							error: function(er) {
								BackErr(er);
							}
						});
					}
			})
   });
   //切换编辑器
   $("[lay-submit]").click(function(event) {
       if($(this).text()=="立即提交"){
          getContent()
       }
   });

})
$(".tab_radio").each(function(index, element) {
	var T=$(this);
	var box=$("[tagbox]")
		$(".tab_radio").delegate(".layui-form-radio","click",function(e){
		var index=$(".tab_radio .layui-form-radio").index(this)
		box.removeClass("show")
		box.eq(index).addClass("show")
	})
   
	//box.eq(0).addClass("show")
});
//弹出层
window.pages=1;
window.id=[];
window.typeid=[];
window.total=1;
Multiselect=false;
var layAjax={
  init:function(){
    $("[selectid]").click(function(){
      $(this).val("");
      layAjax.pageLock.show(1000,".lay_select",".lay_select .close")
    })
    $(".sear_btn").click(function(){
      var data={"s_type":$(".typeid").val(),"keyword":$(".keyword").val(),"pages":1}
      layAjax.ajax(data)
    })
    $(".layui-btn-danger").click(function(){
      $("[selectid]").val(window.id);
      $("[typeid]").val(window.typeid);
      layAjax.pageLock.reHide(".lay_select")
    })
     $(".lay_select").unbind("click").delegate("li:not('.active')","click",function(){
          var _self=$(this);
          if(Multiselect){
            window.id.push(_self.data("id"));
          }
          else
          {
            $(".lay_select li").removeClass("active")
            window.id=_self.data("id");
            window.typeid=_self.data("typeid");
          }
           _self.addClass("active");
          console.log(window.typeid)
       })
  },
  ajax:function(data){
    var url=$(".lay_select form").attr("action");
    $.ajax({
       url:url,
      data:data,
      type: "get",
      dataType: "json",
      success: function(data) {
        layAjax.domInit(data)
      },
      error: function(er) {
        console.log(er)
      }
    });
  },
  domInit:function(data){
    var li="";
    var list=$(".lay_select .list");
    window.total=data.total;
    list.html('');
    if(data.rescode==0){
      for(var i=0;i<data.list.length;i++){
       var item=data.list;
       li+=" <li data-id ="+item[i].id+" data-typeid ="+item[i].typeid+"><span>ID:"+item[i].id+"</span><span class=\"type\">["+item[i].type+"]</span><span class=\"title\">"+item[i].title+"</span></li>"
      }
      $(li).appendTo(list);
      if(window.pages==1){
        layAjax.dom_page();
      }
    }
  },
  dom_page:function(){
     layui.use(['laypage', 'layer'], function(){
        var laypage = layui.laypage
        ,layer = layui.layer;
        laypage({
          cont: 'demo1'
          ,pages: window.total //总页数
          ,groups: 5 //连续显示分页数
          ,jump: function(obj){
             if(obj.curr!=window.pages){
                window.pages=obj.curr;
                var data={"s_type":$(".typeid").val(),"keyword":$(".keyword").val(),"pages":obj.curr}
                layAjax.ajax(data)
             }
          }
        });
      });
  },
  pageLock:{
    show:function(index,ceng,colse){
      var $width=($(window).width()-$(ceng).outerWidth())/2
      var $height=($(window).height()-$(ceng).outerHeight())/2
        if(!$("#lockpage").length){
      var lockhtml=$("<div id='lockpage'/>").css({position:"absolute",zIndex:(index?index:20),left:0,top:0,width:'100%',height:$(document).height(),background:"#000",opacity:0.3}).appendTo("body");
      }
      $("#lockpage").css({zIndex:index?index:20,opacity:0,display:"block"}).css({opacity:0.8});
        $(ceng).css({position:"fixed",zIndex:index+1,left:$width,top:$height,display:"block"});
      layAjax.pageLock.hide(colse,ceng)
    },
    hide:function(colse,ceng){
       $(colse).click(function(e){
           $(ceng).hide();
           $("#lockpage").remove()
           e.stopPropagation();
           $("body")[0].ontouchmove=function(){
              };
      return false
    });
    },
    reHide:function(ceng){
        $("#lockpage").remove();
        $(ceng).hide();
        $("body")[0].ontouchmove=function(){
              };
        return false
    }
  }
}
function getContent() {  
    if(UE.getEditor("editor").queryCommandState('source')!=0){
       UE.getEditor("editor").execCommand('source'); 
    }
    if(UE.getEditor("waitArticle").queryCommandState('source')!=0){
       UE.getEditor("waitArticle").execCommand('source'); 
    }
    if(UE.getEditor("liveData").queryCommandState('source')!=0){
       UE.getEditor("liveData").execCommand('source'); 
    }
} 




